package com.vito.oa.act.extend.action;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.act.approval.model.ActApprovalProgressModel;
import com.vito.act.model.ActExBussModel;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.oa.act.extend.model.OaActExBussModel;
import com.vito.oa.act.extend.service.OaActApprovalProgressService;
import com.vito.oa.act.model.ActUserProcessSettingModel;

/**
 * OA-审批-流程
 *
 * @author renhh
 * @createdata 2016年11月22日
 */
@Controller
@RequestMapping("oa/act/approval/progress")
public class OaActApprovalProgressAction extends BaseAction{
	private static final Logger log = LoggerFactory.getLogger(OaActApprovalProgressAction.class);
	
	@Reference
	private OaActApprovalProgressService oaApprovalProgressService;
	@ResponseBody
	@RequestMapping(value = "queryProgress")
	public Result queryProgress(OaActExBussModel model) {
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(null==user){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			String deptId = user.getDeptParentId();
			model.setCreateDept(deptId);;
			r.setData(oaApprovalProgressService.queryProgressByPage(model));
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
		return r;
	}
	@ResponseBody
	@RequestMapping(value = "saveProgress")
	public Result saveProgress(@RequestBody OaActExBussModel model) {
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(null==user){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			if(Validata.isNullOrEmpty(model.getBusinessName())){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(MessageFormat.format(ResMsg.MSG_DATA_NEED, "流程名称"));
				return r;
			}
			if(Validata.isNullOrEmpty(model.getIconUrl())){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(MessageFormat.format(ResMsg.MSG_DATA_NEED, "流程图片"));
				return r;
			}
			if(Validata.isNullOrEmpty(model.getMsgType())){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(MessageFormat.format(ResMsg.MSG_DATA_NEED, "消息类型"));
				return r;
			}
			if(model.getNodes().isEmpty()){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(MessageFormat.format(ResMsg.MSG_DATA_NEED, "流程节点"));
				return r;
			}
			if(Validata.isNullOrEmpty(model.getFormId())){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(MessageFormat.format(ResMsg.MSG_DATA_NEED, "关联表单"));
				return r;
			}
			model.setProcessName(model.getBusinessName());
			String deptId = user.getDeptParentId();
			String userId = user.getUserId();
			model.setCreateDept(deptId);
			model.setCreateUser(userId);
			r = oaApprovalProgressService.addProgress(model);
			String key = (String)r.getData();
			if(Validata.notNullOrEmpty(key)){
				ActUserProcessSettingModel setting = new ActUserProcessSettingModel();
				model.setProcessKey(key);
				setting.setProcess(model);
				setting.setCompanyId(deptId);
				setting.setStatus("0");
				oaApprovalProgressService.addProcessToCompany(setting, userId);
			}
			return r;
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
	}
	@ResponseBody
	@RequestMapping(value = "addProcessToCompany")
	public Result addProcessToCompany(String key) {
		Result r = new Result();
		try {
			if(key==null||key.isEmpty()){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(ResMsg.E31);
				return r;
			}
			UserModel user = SessionMap.getObj();
			if(null==user){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			ActUserProcessSettingModel procSetting = new ActUserProcessSettingModel();
			OaActExBussModel model = new OaActExBussModel();
			model.setProcessKey(key);
			procSetting.setCompanyId(user.getDeptParentId());
			procSetting.setProcess(model);
			oaApprovalProgressService.addProcessToCompany(procSetting,user.getUserId());
			r.setCode(Result.CODE_OK);
			r.setMsg(ResMsg.EXE_SUCCESS);
		} catch (Exception e) {
			log.error("导入异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
		return r;
	}
	@ResponseBody
	@RequestMapping(value = "updateProgressStatus")
	public Result updateProgressStatus(@RequestParam Map<String, String> map) {
		Result r = new Result();
		try {
			if(map==null||map.isEmpty()){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(ResMsg.E31);
				return r;
			}
			UserModel user = SessionMap.getObj();
			if(null==user){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			map.put("deptId", user.getDeptParentId());
			map.put("operUser", user.getUserId());
			r.setData(oaApprovalProgressService.updateProgressStatus(map));
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("更新异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
		return r;
	}
	@ResponseBody
	@RequestMapping(value = "updateProgressOrder")
	public Result updateProgressOrder(@RequestBody List<Map<String, String>> list) {
		Result r = new Result();
		try {
			if(list==null||list.size()==0){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(ResMsg.E31);
				return r;
			}
			UserModel user = SessionMap.getObj();
			if(null==user){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			r.setData(oaApprovalProgressService.updateProgressOrder(list,user.getDeptParentId(),user.getUserId()));
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("更新异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
		return r;
	}
	@ResponseBody
	@RequestMapping(value = "queryProcessDataCount")
	public Result queryProcessDataCount(String processKey) {
		Result r = new Result();
		try {
			
			r.setData(oaApprovalProgressService.queryProcessDataCount(processKey));
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
		return r;
	}
	@ResponseBody
	@RequestMapping(value = "deleteProgress")
	public Result deleteProgress(ActExBussModel actExBussModel) {
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(null==user){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			actExBussModel.setCreateDept(user.getDeptParentId());
			return oaApprovalProgressService.deleteProgress(actExBussModel);
		} catch (Exception e) {
			log.error("自定义流程删除异常，key={}",actExBussModel.getProcessKey(),e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
	}
	@ResponseBody
	@RequestMapping(value = "updateProgressBaseMsg")
	public Result updateProgressBaseMsg(ActApprovalProgressModel model) {
		Result r = new Result();
		try {
			if(oaApprovalProgressService.updateProgressBaseMsg(model)>0){
				r.setCode(Result.CODE_OK);
				r.setMsg(ResMsg.UPDSUC);
			}else{
				r.setCode(Result.CODE_OK);
				r.setMsg(ResMsg.UPDERR);
			}
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
		}
		return r;
	}
}
